//
//  CustomViewController.m
//  MaJangJi
//
//  Created by 嗯，大葱 on 16/9/9.
//  Copyright © 2016年 嗯，大葱. All rights reserved.
//

#import "CustomViewController.h"
#import "LeftTitleTableViewCell.h"
#import "RightCollectionViewCell.h"
#import "DetailViewController.h"
#define k_Width      [UIScreen mainScreen].bounds.size.width

#define h_Height     [UIScreen mainScreen].bounds.size.height
@interface CustomViewController ()<UITableViewDelegate, UITableViewDataSource,UICollectionViewDelegate, UICollectionViewDataSource>
{
    BOOL tempFlag;
    CGFloat tempY;
}

@property (nonatomic, strong)UITableView *wnsZtableView;

@property (nonatomic, strong)UICollectionView *wnsZcollectionView;

@property (nonatomic, strong)NSArray *titleArray;

@property (nonatomic, strong)UIButton *submitBtn;

@property (nonatomic, strong)UIView *leftView;
@end

@implementation CustomViewController

- (void)viewWillAppear:(BOOL)animated {
    tempY = 64;
    NSIndexPath * selIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    [_wnsZtableView selectRowAtIndexPath:selIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
    NSIndexPath * path = [NSIndexPath indexPathForItem:0 inSection:0];
    [self tableView:self.wnsZtableView didSelectRowAtIndexPath:path];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    tempFlag = NO;
    [self setNaVBar];
    [self configureViewController];
    _titleArray = @[@"麻将机",@"棋牌",@"椅子",@"茶具",@"配件",@"地板",@"壁纸",@"装饰"];

}
- (void)setNaVBar {
    self.navigationItem.title = @"选择物料";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(handleBack2)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor grayColor];
    UIButton *rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightBtn.frame = CGRectMake(5, 5, 40, 20);
    [rightBtn setTitle:@"选择" forState:(UIControlStateNormal)];
    [rightBtn setTitle:@"取消" forState:(UIControlStateSelected)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    rightBtn.backgroundColor = [UIColor orangeColor];
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn addTarget:self action:@selector(hanldeRightBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.rightBarButtonItem = menuButton;

    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, -20, k_Width, 20)];
//    statusBarView.backgroundColor=[UIColor colorWithRed:82/255.0 green:82/255.0 blue:82/255.0 alpha:1.0];
//    [self.navigationController.navigationBar addSubview:statusBarView];
}
#pragma mark导航栏按钮点击事件
- (void)handleBack2 {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)hanldeRightBtnAction:(UIButton *)sehnder  {
    sehnder.selected = !sehnder.selected;
    if (sehnder.selected == YES) {
        tempFlag = YES;
        [self.wnsZcollectionView reloadData];
    }
    else {
        tempFlag = NO;
        [self.wnsZcollectionView reloadData];
    }
   
}
#pragma mark 配置控制器视图
- (void)configureViewController {
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1.0];
    _wnsZtableView = [[UITableView alloc] initWithFrame:CGRectMake(3, 0, 100, h_Height) style:(UITableViewStylePlain)];
    _wnsZtableView.delegate = self;
    _wnsZtableView.dataSource = self;
     _wnsZtableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _wnsZtableView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1.0];

    [self.view addSubview:_wnsZtableView];
    [self.wnsZtableView registerNib:[UINib nibWithNibName:@"LeftTitleTableViewCell" bundle:nil] forCellReuseIdentifier:@"title"];
    
    
    //最左侧红线
    
    _leftView = [[UIView alloc] initWithFrame:CGRectMake(0,tempY,3,80)];
    _leftView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_leftView];
    
    
    
#pragma mark 右侧视图
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置每一个item的大小;根据不同的屏幕尺寸适配
    layout.itemSize = CGSizeMake((k_Width - CGRectGetMaxX(_wnsZtableView.frame)- 5)/2, 120);
    
    //设置分区的缩进量;
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    //设置行间距;t
    layout.minimumLineSpacing = 5;
    //设置最小的左右间距
    layout.minimumInteritemSpacing = 1;
    
    _wnsZcollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_wnsZtableView.frame), 64, k_Width - CGRectGetMaxX(_wnsZtableView.frame), h_Height - 64 - 40) collectionViewLayout:layout];
    //dataSource -- 提供数据;
    _wnsZcollectionView.dataSource = self;
    _wnsZcollectionView.delegate = self;
    [self.view addSubview:_wnsZcollectionView];
    _wnsZcollectionView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1.0];
    [_wnsZcollectionView registerNib:[UINib nibWithNibName:@"RightCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"right"];
    
    _submitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _submitBtn.frame = CGRectMake(CGRectGetMaxX(_wnsZtableView.frame) , h_Height - 40, k_Width - CGRectGetMaxX(_wnsZtableView.frame), 40);
    [_submitBtn setImage:[UIImage imageNamed:@"777"] forState:(UIControlStateNormal)];
    [_submitBtn setImage:[UIImage imageNamed:@"666"] forState:(UIControlStateSelected)];
   
    [self.view addSubview:_submitBtn];
}


#pragma mark tableView代理事件
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"title"];
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     CGRect  popoverRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];
    
    tempY = popoverRect.origin.y;
    [self changeY];
    
    
}
- (void)changeY{
    _leftView.frame = CGRectMake(0,tempY,3,80);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
     _submitBtn.selected = tempFlag;
    RightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"right" forIndexPath:indexPath];
    if (tempFlag == NO) {
        cell.selectBtn.alpha = 0;
        cell.selectBtn.selected = NO;
        cell.selectBtn.userInteractionEnabled = NO;
    }
    if (tempFlag == YES) {
        cell.selectBtn.userInteractionEnabled = YES;
        cell.selectBtn.alpha = 1;
        [cell.selectBtn addTarget:self action:@selector(handleSelectBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return cell;
}
//右侧视图的选中按钮的点击事件；
- (void)handleSelectBtnAction:(UIButton *)sender {
    RightCollectionViewCell  * cell = (RightCollectionViewCell *)[[sender superview] superview];
    cell.selectBtn.selected = !cell.selectBtn.selected;
    NSIndexPath * indexPath = [self.wnsZcollectionView indexPathForCell:cell];
    NSLog(@"你点击的是第%@个collectionView",indexPath);
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (tempFlag == YES) {
        return;
    }else {
        DetailViewController *detail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        [self presentViewController:detail animated:YES completion:nil];
        
        NSLog(@"我能进入详情界面的啊哈哈哈哈");
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
